<?php
class Control_DataAnalysis_getMonthcardInfo {

	function getMonthcardInfo() {
		$nowGame = $game 	= MooForm::request('game');
		$action 	= MooForm::request('action');
		$game = MooObj::get('Game')->transportGame($game);
		
		$nowChannel =  $channel 	= MooForm::request('channel');
		$platform 	= MooForm::request('platform');
		$type 		= MooForm::request('type');
		$dataType 		= MooForm::request('dataType');
		
		if ($action == 0) {
			MooView::render('getMonthcardInfo');
		} else {
			
			// 总购买人数
			$getTotalMonthcard = MooConfig::get('main.rechargeUrl.getTotalMonthcard');
			
			// 一次购买用户
			$getServerMonthcardNums = MooConfig::get('main.rechargeUrl.getServerMonthcardNums');
			
			// 多次购买用户
			$getReBuyMonthcardNums = MooConfig::get('main.rechargeUrl.getReBuyMonthcardNums');
			
			$getTotalMonthcard 		= $getTotalMonthcard . "&game=". $game;
			$getServerMonthcardNums = $getServerMonthcardNums . "&game=". $game;
			$getReBuyMonthcardNums 	= $getReBuyMonthcardNums . "&game=". $game;
			
			$getTotalMonthcardData = MooUtil::curl_send($getTotalMonthcard, null);
			
			$getServerMonthcardNumsData = MooUtil::curl_send($getServerMonthcardNums, null);
			$getReBuyMonthcardNumsData = MooUtil::curl_send($getReBuyMonthcardNums, null);
			
			if($getTotalMonthcardData) {
				$getTotalMonthcardData = MooJson::decode($getTotalMonthcardData);
			}
			if($getServerMonthcardNumsData) {
				$getServerMonthcardNumsData = MooJson::decode($getServerMonthcardNumsData);
			}
			if($getReBuyMonthcardNumsData) {
				$getReBuyMonthcardNumsData = MooJson::decode($getReBuyMonthcardNumsData);
				
				foreach ($getReBuyMonthcardNumsData as $key => $val) {
					$formatGetReBuyMonthcardNumsData[$val['server']] = $val['reBuyTimes'];
				}
			}
			
			$formatDatas = array();
			foreach ($getServerMonthcardNumsData as $key => $val) {
				$serverId =  $val['server'];
				$buytimes = $val['buyTimes'];
				$psInfo = explode("-", $serverId);
				$data['platform']	 = $psInfo[0];
				$data['serverId']	 = $psInfo[1];
				$data['server']	 	= $serverId;
				$data['buyTimes'] 	= $buytimes;
				$reBuyTimes 		= isset($formatGetReBuyMonthcardNumsData[$serverId]) ? $formatGetReBuyMonthcardNumsData[$serverId] : 0;
				$data['reBuyTimes'] = $reBuyTimes;
				$data['buyPer'] 	= MooUtil::getPercent($reBuyTimes, $buytimes);
				$formatDatas[] = $data;
			}
			
			MooArray::tdSort($formatDatas, "platform", SORT_ASC, "serverId", SORT_ASC);
			
			/* $viewData = MooJson::encode($rechargeNumData2);
			// 格式化数据
			$viewData = str_replace(',"',',',$viewData);
			$viewData = str_replace('"]',']',$viewData); */
			
			if($formatDatas && $type == "daochu") {
				$excelData = array();
				
				$sDate = date("Ymd", strtotime($beginDate));
				$eDate = date("Ymd", strtotime($endDate));
				
				$name = "月卡统计";
				
				$excelRs = array(
					0 => array(
						0 => '',
						1 => $name,
					),
					1 => array(
						0 => '服',
						1 => '购买次数',
						2 => '二次购买次数',
						3 => '二次购买率',
				));
				
				foreach ($formatDatas as $key=>$val) {
					$arr[] = $val['server'];
					$arr[] = $val['buyTimes'];
					$arr[] = $val['reBuyTimes'];
					$arr[] = $val['buyPer'];
					$excelData[] = $arr;
					$arr = array();
				}
				
				$excelData = array_merge($excelRs, $excelData);
				$writeExcelArr[0]['name'] = $name;
				$writeExcelArr[0]['data'] = $excelData;
					
				$excelPath = ROOT_PATH . "/export/orders/" . $name . ".xls";
				$rs = MooObj::get('Control_ExcelConfMaker_Excel')->write($excelPath, $writeExcelArr);
				$this->downFile($excelPath, $name . ".xls");
				exit;
			}
			
			MooView::set('viewData', $viewData);
			MooView::set('data', $formatDatas);
			MooView::set('nowGame', $nowGame);
			MooView::render('getMonthcardInfo');
		}
	}

	 // 下载文件
	 private function downFile($filepath, $fileName) {
		$fp			=	fopen($filepath,"r"); 
		$file_size	=	filesize($filepath); 
		//下载文件需要用到的头 
		header("Content-type: application/octet-stream"); 
		header("Accept-Ranges: bytes"); 
		header("Accept-Length:".$file_size); 
		header("Content-Disposition: attachment; filename=".$fileName); 
		$buffer		=	1024; 
		$file_count	=	0; 
		//向浏览器返回数据 
		while(!feof($fp) && $file_count < $file_size){ 
		$file_con	=	fread($fp,$buffer); 
		$file_count	+=	$buffer; 
		echo $file_con; 
		} 
		fclose($fp); 
	}
	
}